package com.sy.sevice.impl;

import com.sy.constant.AppConstant;
import com.sy.constant.BusinessConstant;

import com.sy.constant.RegexConstant;
import com.sy.dto.JsonResult;
import com.sy.entity.Student;
import com.sy.mapper.ClassMapper;
import com.sy.mapper.StudentMapper;
import com.sy.sevice.StudentService;
import com.sy.utils.CommonUtils;
import com.sy.vo.AddClassAndStudentVo;
import com.sy.vo.AddStudentVo;
import com.sy.utils.CommonUtils;
import com.sy.vo.LoginUserVo;
import com.sy.vo.ModifyStudentVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Monkey-D-Gu
 * @version 1.0
 * @date 2020/9/11 0011 13:48
 * @usage 学生类相关的Service实现类
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CommonUtils commonUtils;


    @Autowired
    private ClassMapper classMapper;

    /**
     * 添加新的学生信息
     *
     * @param addStudentVo 学生信息携带对象
     * @param addUserVo 学生信息携带对象
     * @return 添加成功的学生信息
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JsonResult addStudent(AddStudentVo addStudentVo) {
        //学生姓名
        String username = addStudentVo.getUsername();
        //班级id
        Integer classId = addStudentVo.getClassId();
        //性别
        Integer gender = addStudentVo.getGender();
        //实例化学生对象
        Student student = new Student();
        student.setUsername(username);
        //密码置成默认密码
        student.setPassword(BusinessConstant.DEFALUT_PASSWORD);
        //性别
        student.setGender(gender);
        student.setClassId(classId);
        //添加学生信息
        boolean addSuccess = studentMapper.addStudent(student);
        System.out.println("是否更新学生信息成功：" + addSuccess);
        //获取自增主键id
        Integer studentId = student.getId();
        //获取所属班级的表名
        String tableName = CommonUtils.getTableName(classId);
        //新增实体类携带参数
        AddClassAndStudentVo addClassAndStudentVo = new AddClassAndStudentVo();
        addClassAndStudentVo.setTableName(tableName);
        addClassAndStudentVo.setStudentId(studentId);
        //新增班级-学生表信息
        boolean addTableSuccess = classMapper.addClassAndStudent(addClassAndStudentVo);
        System.out.println("是否更新班级-学生表信息成功：" + addTableSuccess);
        //获取自增主键id
        Integer id = addClassAndStudentVo.getId();
        String studentNo = CommonUtils.createStudentNo(classId, studentId,id);
        student.setStudentNo(studentNo);
        //将学号更新进学生表中
        boolean addStudentNoSuccess = studentMapper.updateStudentNo(student);
        System.out.println("是否更新学生学号成功：" + addStudentNoSuccess);
        //将密码保密
        student.setPassword(BusinessConstant.HIDE_PASSWORD);
        //添加学生信息成功，返回201状态码和学生信息
        return JsonResult.buildSuccess(201,student);
    }

    /**
     * @author JianBo-Shneg
     * @version 1.0
     * @date 2020/9/14 0011 13:48
     * 修改学生个人信息
     * @param modifyStudentVo 学生信息携带对象
     * @return 修改成功的学生信息
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JsonResult modifyStudent(ModifyStudentVo modifyStudentVo) {
        String username = modifyStudentVo.getUsername();
        String oldPwd = modifyStudentVo.getOldPwd();
        String password = modifyStudentVo.getPassword();
        String newPwdConfirm = modifyStudentVo.getNewPwdConfirm();
        String phone = modifyStudentVo.getPhone();
        String email = modifyStudentVo.getEmail();


        //TODO:获得当前登入学生的id
        modifyStudentVo.setId(4);

        //用户名
        //判断
        if (StringUtils.isBlank(username)) {
            return JsonResult.buildFailure(400, "用户名不能为空");
        }
        if (!commonUtils.validateRegex(username, RegexConstant.USER_NAME_REGEX)) {
            return JsonResult.buildFailure(400, "用户名不合法");
        }
        //手机号码
        if (StringUtils.isBlank(phone)) {
            return JsonResult.buildFailure(400, "手机号不能为空");
        }
        if (!commonUtils.validateRegex(phone, RegexConstant.PHONE_NUMBER_REGEX)) {
            return JsonResult.buildFailure(400, "手机号不合法");
        }
        //当前密码
        //获得当前学生的密码
        if (StringUtils.isBlank(oldPwd)) {
            return JsonResult.buildFailure(400, "密码不能为空");
        }
        //TODO:根据学生id查询学生密码
        String pwdByStudentId = studentMapper.getPwdByStudentId(4);

        if (!oldPwd.equals(pwdByStudentId)) {
            return JsonResult.buildFailure(400, "输入的密码不对");
        }
        //新密码
        if (StringUtils.isBlank(password)) {
            return JsonResult.buildFailure(400, "密码不能为空");
        }
        if (!password.equals(newPwdConfirm)) {
            return JsonResult.buildFailure(400, "两次输入的密码不一致");
        }
        if (!commonUtils.validateRegex(password, RegexConstant.STRONG_PWD_REGEX)) {
            return JsonResult.buildFailure(400, "密码不是强密码");
        }
        //邮箱
        if (StringUtils.isBlank(email)) {
            return JsonResult.buildFailure(400, "邮箱不能为空");
        }
        if (!commonUtils.validateRegex(email, RegexConstant.EMAIL_NUMBER_REGEX)) {
            return JsonResult.buildFailure(400, "邮箱不合法");
        }
        //将修改后的信息更新到数据库
        boolean isSuccess = studentMapper.updateStudent(modifyStudentVo);
        if (isSuccess) {
            return JsonResult.buildSuccess(200, modifyStudentVo);
        }
        return JsonResult.buildFailure(400, "修改个人信息失败");

    }
}
